package com.example.myapplication;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class BlankFragment1 extends Fragment {
    RecyclerView recyclerView;
    List list;
    Myadapter adapter;
    Context context;
    View view;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.activity_main1,container,false);
        recyclerView=view.findViewById(R.id.recycleview);//连接至tab1的控件
        //创建数据
        String[] names={"爸爸","妈妈","姐姐","弟弟","西西","小方","小杨","老师","叔叔","爷爷"};
        int[] images={R.drawable.weixinone,R.drawable.weixinone,R.drawable.weixinone,R.drawable.weixinone,R.drawable.weixinone,
                R.drawable.weixinone,R.drawable.weixinone,R.drawable.weixinone,R.drawable.weixinone,R.drawable.weixinone};
        String[] phones={"13420827777","13423459999","18920203433","13712930000","13611119898",
                "17238389999","17326357489","13482930203","13619872034","18923458976"};
        String[] regions={"福建 福州","福建 福州","福建 福州","福建 福州","福建 福州","湖北 武汉","湖北 武汉",
                "福建 福州","福建 福州","福建 福州"};
        List<Map<String,Object>> items=new ArrayList<Map<String,Object>>();
        for(int i=0;i<names.length;i++){
            Map<String,Object> item=new HashMap<String, Object>();
            item.put("i_name",names[i]);
            item.put("i_image",images[i]);
            item.put("i_phone",phones[i]);
            item.put("i_region",regions[i]);
            items.add(item);
        }

        //创建RecycleView实例和设置Adapter
        Context context=getContext();
        adapter=new Myadapter(view.getContext(),items);//把list导入
        LinearLayoutManager manager=new LinearLayoutManager(view.getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        // Inflate the layout for this fragment

        //实现拖拽和左滑删除效果
        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                int swiped=ItemTouchHelper.LEFT;
                int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                return makeMovementFlags(dragFlags,swiped);
            }

            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                int oldPosition = viewHolder.getAdapterPosition();
                int newPosition = target.getAdapterPosition();
                if (oldPosition < newPosition) {
                    for (int i = oldPosition; i < newPosition; i++) {
                        // 改变数据集
                        Collections.swap(items, i, i +1);
                    }
                } else {
                    for (int i = oldPosition; i > newPosition; i--) {
                        // 改变数据集
                        Collections.swap(items, i, i - 1);
                    }
                }
                adapter.notifyItemMoved(oldPosition, newPosition);
                return true;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                int position = viewHolder.getAdapterPosition();
                items.remove(position);
                adapter.notifyItemRemoved(position);
            }
            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                if (actionState!=ItemTouchHelper.ACTION_STATE_IDLE){
                    viewHolder.itemView.setBackgroundColor(Color.parseColor("#04BE02"));
                }
            }
            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                viewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
            }
        });
        //关联recycleView
        itemTouchHelper.attachToRecyclerView(recyclerView);
        return view;
    }

}